#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include "my_math.h"
#include "play.h"
#include "my_conio.h"
#include "key_map.h"

const size_t Fs = SAMPLING_RATE; // samping rate
const double dT = 1.0 / Fs;		 // sampling period
const size_t T = 1;				 // time
const size_t N = T * Fs;		 // samples count
double f = 5000;				 // frequency
double t[N], y[N];

unsigned char buf[N];

int data[5000] = {
#include "data.array"
};
int freq_list[100] = {
#include "freq_list.array"

};
int loud[100] = {
	100, 90, 80, 70, 60, 1};
int dura[100] = {
#include "dura.array"
};

int main()
{
	size_t s, t;
	my_sin(0); // initialize sin table
	// int freq_list[24];
	// for (int i = 0; i < 24; i++)
	//     freq_list[i] = int(110 * pow(2, i / 12.0));

	//freopen("music.txt", "r", stdin);
	int instru_no;	// 0 - piano, 1 - acoordion
	int freq_index; // 1 ~ 24
	int time_ms;	// time: ms
	int song_id = 1;
	int start_pos;
	int speed;
	int real_speed;
	int loudness;
	int fr;
	int mode;
	char ch;
	printf("请选择模式\n0 - 自动弹奏，1 - 键盘输入\n");
	std::cin >> mode;
	if (mode == 1)
	{
		int start = 1;
		int FREQ = 0;
		int sing = 0;
		instru_no = 0;
		printf("请选择整体频率\n0为正常，1为整体高八度，-1为整体低八度，以此类推\n");
		std::cin >> fr;
		printf("请选择音量大小\n范围1~100\n");
		std::cin >> loudness;
		printf("按动键盘q~u,a~j开始弹奏\n1、2、3对应切换乐器为钢琴，手风琴，小提琴\n按“.”结束程序\n");
		double freq_list2[sizeof(freq_list)];
		for (int i = 0; i < sizeof(freq_list); i++)
			freq_list2[i] = freq_list[i] * pow(2, fr);
		set_freq_list(freq_list2); // set freq list
		while (1)
		{
			ch = getch();
			int key_pos = key_map(ch);
			sing = 0;
			if (ch >= '1' && ch <= '3')
			{
				instru_no = ch - '0' - 1;
			}
			else if (key_pos >= 1 && key_pos <= 7) //qwertyu
			{
				sing = 1;
				FREQ = key_pos;
			}
			else if (key_pos >= 11 && key_pos <= 17) //asdfghj
			{
				sing = 1;
				FREQ = key_pos + 4;
			}
			else if (ch == '.')
			{
				return 0;
			}
			if (sing == 1)
				play(instru_no, FREQ, dura[1] / 1000, loudness);
		}
	}
	while (1)
	{
		printf("请选择乐器\n1为钢琴，2为手风琴，3为小提琴，4为混合\n");
		std::cin >> instru_no;
		instru_no--;
		printf("请选择歌曲\n1.《He's a Pirate》\n2.《Empire of Star Sky》 [doge]\n3.《Game of Thrones Theme》\n");
		std::cin >> song_id;
		printf("请选择整体频率\n0为正常，1为整体高八度，-1为整体低八度，以此类推\n");
		std::cin >> fr;
		printf("请选择整体速度\n100为正常，数值越低速度越快，数值越大速度越慢\n");
		std::cin >> real_speed;
		printf("请选择音量大小\n范围1~100\n");
		std::cin >> loudness;
		double freq_list2[sizeof(freq_list)];
		for (int i = 0; i < sizeof(freq_list); i++)
			freq_list2[i] = freq_list[i] * pow(2, fr);
		set_freq_list(freq_list2); // set freq list
		switch (song_id)
		{
		case 1:
			start_pos = 0;
			speed = 100;
			break;
		case 2:
			start_pos = 297;
			speed = 75;
			break;
		case 3:
			start_pos = 566;
			speed = 50;
			break;
		}
		int pos = start_pos;
		int FREQ = 0;
		int LOUD = 0;
		int DURA = 0;
		int remix = 0;
		if (instru_no == 3)
			remix = 1;
		while (1)
		{
			FREQ = data[4 * pos];
			LOUD = data[4 * pos + 1];
			DURA = data[4 * pos + 2];

			if (FREQ == 999)
				break;
			//-----------
			//FREQ=freq[FREQ];
			LOUD = loud[LOUD];
			DURA = dura[DURA];
			//----------

			if (remix == 1)
				instru_no = data[4 * pos + 3];
			if (instru_no != 2)
				LOUD = LOUD / 2;
			DURA = DURA * speed / 100;
			LOUD = LOUD * loudness / 100;
			time_ms = DURA / 1000 * real_speed / 100;
			//loudness=LOUD*loudness/100/5;
			//  DURA*Fs/1000000=N
			play(instru_no, FREQ, time_ms, LOUD);
			pos++;
		}
		break;
	}
	return 0;
}
